package com.bo.atomicity;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: gpb
 * @Date: 2024/7/25 06:58
 * @Description:
 */
public class ILock {
    static java.util.concurrent.locks.Lock lock = new ReentrantLock();
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                for (int i = 0; i < 100; i++) {
                    count++;
                }
            } catch (Exception e) {
            } finally {
                lock.unlock();
            }
        });
        Thread t2 = new Thread(() -> {
            lock.lock();
            try {
                for (int i = 0; i < 100; i++) {
                    count++;
                }
            } catch (Exception e) {
            } finally {
                lock.unlock();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
    }
}
